<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use think\Session;

class Purcha extends Model
{
    // 表名
    protected $name = 'purcha';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'status_text'
    ];
    public function getPublisherAttr($value){
       $model = model('admin');
       $info = $model->where(array('userid'=>$value))->find();
       return $info['username'];
    }

    public function getOrderId()
    {
        $order_id = "CG-" . date('Ymd') . '-' . substr(time(),6,4).random(4, 1);
        $info = $this->where(array('order_id' => $order_id))->find();

        if ($info) {
            $this->getOrderId();
        } else {
            return $order_id;
        }
    }
    
    public function getStatusList()
    {
        return ['1' => __('Status 1'),'2' => __('Status 2'),'3' => __('Status 3'),'4' => __('Status 4')];
    }     
    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

}
